﻿using System.Windows;
using Caliburn.Micro;
using JY.Wpf.Core.Events;
using JY.Wpf.Core.Models;
using JY.Wpf.Core.Tools;

namespace JY.Wpf.Core.ViewModelBases
{
    /// <summary>
    /// 窗体页面ViewModel
    /// </summary>
    public class WindowViewModelBase : ViewModelBase, IHandle<BusyIndicatorEvent>, IHandle<WarningMessageEvent>
    {
        #region # 变量
        
        #endregion

        #region # 构造器
        /// <summary>
        /// 构造器
        /// </summary>
        public WindowViewModelBase()
        {
            this.EventAggregator.Subscribe(this, true);
            this.IsOpen = true;
            this.Width = 1200;
            this.Height = 600;
        }

        #endregion

        #region # 属性
        #region # 是否打开——bool IsOpen
        private bool _isOpen;
        /// <summary>
        /// 是否打开
        /// </summary>
        public bool IsOpen
        {
            get { return this._isOpen; }
            set
            {
                this._isOpen = value;
                if (!this._isOpen)
                {
                    this.Deactivate();
                }
                base.NotifyOfPropertyChange();
            }
        }
        #endregion

        #region # 宽度——double Width
        /// <summary>
        /// 宽度
        /// </summary>
        [NotifyPropertyChanged]
        public double Width { get; set; }
        #endregion

        #region # 高度——double Height
        /// <summary>
        /// 高度
        /// </summary>
        [NotifyPropertyChanged]
        public double Height { get; set; }
        #endregion

        #region # 警告消息——WarningMessage WarningMessage
        /// <summary>
        /// 警告消息
        /// </summary>
        [NotifyPropertyChanged]
        public WarningMessage WarningMessage { get; set; }
        #endregion
        #endregion

        #region # Command 方法

        #region # 关闭窗体——void Close()
        /// <summary>
        /// 关闭窗体
        /// </summary>
        public void Close()
        {
            foreach (Window currentWindow in Application.Current.Windows)
            {
                if (currentWindow.DataContext is WindowViewModelBase && currentWindow.Title == this.Title)
                {
                    currentWindow.Close();
                    break;
                }
            }
            this.EventAggregator.Unsubscribe(this);
            this.IsOpen = false;
            this.Deactivate();
        }
        #endregion 
        #endregion

        #region # Events 方法

        #region # 显示遮罩事件方法——void Handle(BusyIndicatorEvent busyIndicatorEvent)
        /// <summary>
        /// 显示遮罩事件方法
        /// </summary>
        /// <param name="eventData">事件模型</param>
        public virtual void Handle(BusyIndicatorEvent eventData)
        {
            Application.Current.BeginInvoke(() =>
            {
                this.IsBusy = eventData.IsBusy;
                this.BusyContent = eventData.BusyContent;
            });
        }
        #endregion

        #region # 警告消息事件方法——void Handle(WarningMessageEvent eventData)
        /// <summary>
        /// 警告消息事件方法
        /// </summary>
        /// <param name="eventData">事件模型</param>
        public virtual void Handle(WarningMessageEvent eventData)
        {
            Application.Current.BeginInvoke(() =>
            {
                this.WarningMessage = new WarningMessage(eventData.Message, eventData.MessageType);
            });
        }
        #endregion
        #endregion

        #region # 方法
        #region # 页面失活——void Deactivate()
        /// <summary>
        /// 页面失活
        /// </summary>
        public override void Deactivate()
        {
            this.EventAggregator.Unsubscribe(this);
            this.OnDeactivate(true);
            base.Deactivate();
        }
        #endregion
        #endregion
    }
}
